#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>
#include <QTranslator>
//#include <QStyleFactory>
#include <QDebug>
#include <QStandardPaths>
#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "loggername.h"
#include <QThread>
#include "singlerun.h"
#include <QMessageBox>
#include "fileutils.h"
//#include <QStyleFactory>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QApplication::setOrganizationName("creality3d");
    QApplication::setOrganizationDomain("cxsw3d.com");
    QApplication::setApplicationName("creality3d slicer");

//    QStringList styles = QStyleFactory::keys();
//    qDebug() << styles; // ("windowsvista", "Windows", "Fusion")

//    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
//    QApplication::setStyle(QStyleFactory::create("Fusion"));

    // 检查是否已启动
    SingleRun singleRun(QApplication::applicationName().toStdString().c_str());
    if (singleRun.isAlreadyRun())
    {
        QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("%1 already running!").arg(QApplication::applicationName()));
        return 0;
    }

//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::DataLocation);
//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
//    qDebug() << QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);

    mkMultiDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
    mkMultiDir(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));

    spdlog::set_level(spdlog::level::trace);
    spdlog::set_pattern("[%Y:%m:%d %T %e] [thread %t] [%l] %v");

    std::string loggerFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation).toStdString()
                             + "/" + LOGGER_NAME + ".log";
    qDebug() << "logger file: " << QString::fromStdString(loggerFile);
    auto logger = spdlog::rotating_logger_mt(LOGGER_NAME, loggerFile, 10485760, 5);

    // trigger flush if the log severity is error or higher
    logger->flush_on(spdlog::level::err);

    logger->info("begin to start");

    qDebug() << "thread id: " << QThread::currentThreadId();

    QString appDirPath = QCoreApplication::applicationDirPath();
    QTranslator *translator = new QTranslator;
    translator->load(appDirPath + "/creality3d.qm");
    a.installTranslator(translator);

//#if defined(Q_OS_WIN)
//    qDebug() << "windows";
//#elif defined(Q_OS_MACOS)
//    qDebug() << "mac";
//#elif defined(Q_OS_LINUX)
//    qDebug() << "linux";
//#endif

    QPixmap pixmap(":/images/splash.png");
    QSplashScreen splash(pixmap);
    splash.show();
    a.processEvents(); // 程序在显示启动画面的同时仍能响应交互时间

    MainWindow w;
    w.show();

    splash.finish(&w); // 在主窗体对象初始化完成后结束启动画面
    return a.exec();
}
